package org.pictual.controller;

import java.util.Vector;

import javax.swing.ImageIcon;

import org.apache.log4j.Logger;
import org.pictual.business.vo.ImagenVO;


public class DatabaseImg {

	protected static Logger log= Logger.getLogger(DatabaseImg.class);
	
	private static MyImage extractSingleImg(String url, int i) {

		try {
			
			String imageName=Controlador.getRuta()+"WebContent/"+url;
			ImageIcon icon= new ImageIcon(imageName); 
			

			log.info(imageName);
			float w=icon.getIconWidth();
			float h=icon.getIconHeight();

			float dif = 0;
			if(w > 150){
				dif = 150 / w;
				w = 150;           
				h = h * dif;
				if(h > 150){
					dif = 150 / h;
					h = 150;
					w = w * dif;
				}

			} else if(h > 150){
				dif = 150 / h;
				h = 150;
				w = w * dif;
			}

			MyImage image = new MyImage(url, url, url, new Integer((int) w), new Integer((int) h));
			return image;
		} catch (Exception e) {
			log.error("No se pudo obtener la miniatura de la imagen");
		}
		return null;
	}
	
	public static Vector<MyImage> getVectorImages(ImagenVO[] imagenes){
		Vector<MyImage> images= new Vector<MyImage>();
		for(ImagenVO image: imagenes){
			log.info("image: "+image);
			if(image!=null){
				if(image.getPath_small()!=null){
					MyImage img=extractSingleImg(image.getPath_small(), 1);
					
					img.setHeight(image.getHeight());
					img.setWidth(image.getWidth());
					
					img.setImgThumbnail(image.getPath_small());
					img.setImgLink(image.getPath_big());
					img.setImgRef(image.getPath_original());
					
					images.add(img);
				}
			}
		}
		return images;
	}
}
